package controllers.admin;

import java.util.ArrayList;
import java.util.List;

import models.City;
import models.Order;
import models.OrderItem;
import models.QueryBean;
import models.RemittanceConfirm;
import models.TransferCompany;
import models.TransferPrice;
import models.RemittanceConfirm.ConfirmStatus;
import play.mvc.Controller;
import play.mvc.With;
import controllers.Secure;
import controllers.ValidationUtil;

/**
 * @author ray.wu
 *
 */
@With(Secure.class)
public class Companys extends Controller {

	public static void indexCompany(){
		List<TransferCompany> companyList = TransferCompany.findAll();
		render(companyList);
	}
	
	/**
	 * 获取公司信息
	 * 
	 * @param transPriceId
	 */
	public static String getTransCompany(long companyId){
		TransferCompany tc = TransferCompany.findById(companyId);
		StringBuilder result = new StringBuilder();
		result.append("companyCode-update:"+tc.companyCode);
		result.append("#");
		result.append("companyName-update:"+tc.companyName);
		result.append("#");
		result.append("address-update:"+tc.address);
		result.append("#");
		result.append("phone-update:"+tc.phone);
		result.append("#");
		result.append("owner-update:"+tc.owner);
		
		return result.toString();
	}
	
	/**
	 * 更新公司信息
	 * 
	 * @return
	 */
	public static String updateTransCompany(long companyId,String companyCode,String companyName,
			String address,String phone,String owner)
	{
		TransferCompany tc = TransferCompany.findById(companyId);
		tc.companyCode = companyCode;
		tc.companyName =companyName;
		tc.address = address;
		tc.phone = phone;
		tc.owner = owner;
		
		tc.save();
		
		return "SUCCESS";
	}
	
	/**
	 * 删除公司信息
	 * 
	 * @param companyId
	 * @return
	 */
	public static String deleteTransCompany(long companyId){
		
		//先删除公司的运价信息
		StringBuilder hql = new StringBuilder("from TransferPrice tp where ");
		hql.append(" tp.company.id="+companyId);
		
		List<TransferPrice> priceList = TransferPrice.find(hql.toString()).fetch();
		if(priceList!=null && priceList.size()>0){
			for(TransferPrice tp : priceList){
				tp.company = null;
				tp.delete();
			}
		}
		
		TransferCompany tc = TransferCompany.findById(companyId);
		tc.delete();
		
		return "SUCCESS";
	}
	
	/**
	 * 创建公司信息
	 * 
	 * @return
	 */
	public static String createTransCompany(String companyCode,String companyName,
			String address,String phone,String owner)
	{
		//检查公司是否已经存在		
		StringBuilder hql = new StringBuilder(" from TransferCompany tc where ");
		hql.append(" tc.companyCode='"+companyCode+"'");
		hql.append(" and tc.companyName='"+companyName+"'");
		
		String result = "SUCCESS";
		
		List<TransferCompany> companyList = TransferCompany.find(hql.toString()).fetch();
		if(companyList != null && companyList.size()>0){
			result = "DUPLICATE";
		}else{
			TransferCompany company = new TransferCompany();
			company.companyCode = companyCode;
			company.companyName =companyName;
			company.address = address;
			company.phone = phone;
			company.owner = owner;
			
			company.save();
		}
		
		return result;
	}
	
}